﻿Imports Microsoft.Web.WebView2.Core
Imports System.Text.RegularExpressions
Imports System.IO


Public Class Form1
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Try
            WebView.Source = New Uri(TextBox1.Text)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
        End Try

    End Sub

    Private Sub WebView_Click(sender As Object, e As EventArgs) Handles WebView.Click

    End Sub

    Private Sub TabControl1_MouseClick(sender As Object, e As MouseEventArgs) Handles TabControl1.MouseClick

    End Sub

    Private Function FormatHtml(s As String) As String
        s = Replace(s, "\u003C", "<")
        s = Replace(s, "\n", vbCrLf)
        s = Replace(s, "\""", """")
        Return s.Substring(1, s.Length - 2)
    End Function
    Private Async Sub GetHtml()
        Dim result = Await WebView.CoreWebView2.ExecuteScriptAsync("document.body.outerHTML")
        TextBox2.Text = FormatHtml(result.ToString)
        If Button1.Enabled Then
            UpdateCatalogue()
        Else
            DownloadContent()
        End If
    End Sub
    Private Sub DownloadContent()
        Dim titleRegex As New Regex(">(.*?章.*?)<")
        Dim title As String = titleRegex.Match(TextBox2.Text).Groups(1).Value
        TextBox3.AppendText(vbCrLf & "标题：" & title)
        Dim contentRegex As New Regex(TextBox4.Text, RegexOptions.Singleline)
        Dim content As String = ContentFormat(contentRegex.Match(TextBox2.Text).Groups(1).Value)
        TextBox3.AppendText(vbCrLf & "正文：" & content.Length)
        File.AppendAllText("小说.txt", title & vbCrLf)
        File.AppendAllText("小说.txt", content & vbCrLf & vbCrLf)
        DownloadPos += 1
    End Sub
    Private Function ContentFormat(s As String) As String
        s = Replace(s, "<br />", vbCrLf)
        s = Replace(s, "<br>", vbCrLf)
        s = Replace(s, "<p>", "    ")
        s = Replace(s, "</p>", vbCrLf)
        s = Regex.Replace(s, "<.*?>", "")

        Return s
    End Function
    Private Function FindContent(s As String) As String
        'Dim maxLength As Long, maxStr As String = ""
        Dim result As New List(Of String)
        For Each mtch As Match In Regex.Matches(s, ">(.*?)<", RegexOptions.Singleline)
            Dim text As String = mtch.Groups(1).Value
            Debug.WriteLine(text)
            result.Add(text)
        Next
        'Return maxStr
        Return Join(result.ToArray, vbCrLf)
    End Function

    Private Sub UpdateCatalogue()
        ListBox1.Items.Clear()
        Dim regex As New Regex("<.*?章.*?>")
        For Each mtch As Match In regex.Matches(TextBox2.Text)
            ListBox1.Items.Add(mtch.Value)
        Next
    End Sub

    Private Sub WebView_NavigationCompleted(sender As Object, e As CoreWebView2NavigationCompletedEventArgs) Handles WebView.NavigationCompleted
        GetHtml()
    End Sub
    Private _downloadPos As Long = 0
    Private Property DownloadPos As Long
        Get
            Return _downloadPos
        End Get
        Set(value As Long)
            _downloadPos = value
            If value < taskList.Count Then
                TextBox3.Text = "第" & value + 1 & "/" & taskList.Count & "条，[" & taskList(value) & "]下载中……"
                WebView.Source = New Uri(New Uri(TextBox1.Text), taskList(DownloadPos))
            Else
                TextBox3.Text = "下载完毕！"
                Dim novelName As String = InputBox("下载完毕！小说叫什么名字？", "保存小说")
                If File.Exists(novelName & ".txt") Then
                    File.Delete(novelName & ".txt")
                End If
                Rename("小说.txt", novelName & ".txt")
                Button1.Enabled = True
            End If
        End Set
    End Property
    Private taskList As New List(Of String)

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Button1.Enabled = False
        taskList.Clear()
        Dim regex As New Regex("href=""(.*?)""")
        For Each obj In ListBox1.SelectedItems
            taskList.Add(regex.Match(obj).Groups(1).Value)
        Next
        TextBox3.Text = "目录" & taskList.Count & "条，开始逐条下载……"
        DownloadPos = 0
        'Dim cmd = String.Format("window.locaiton.href = ""{0}""", taskList(downloadPos))
        'Button1.Enabled = True
    End Sub
End Class
